package com.lws.framework.service;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.lws.framework.dao.BaseDao;
import com.lws.framework.dao.Page;

@Transactional
public abstract class BaseService<T extends Serializable> {

    public static Logger logger = LoggerFactory.getLogger(BaseService.class);

    protected abstract BaseDao<T> getDao();

    public HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public void save(T transientInstance) {
        getDao().save(transientInstance);
    }

    public void delete(Long id) {
        getDao().delete(id);
    }

    public T findById(Long id) {
        return getDao().findById(id);
    }

    public List<T> findByExample(T instance) {
        return getDao().findByExample(instance);
    }

    public List<T> findByProperty(String propertyName, Object value) {
        return getDao().findByProperty(propertyName, value);
    }

    public List<T> findAll() {
        return getDao().findAll();
    }

    public void update(T detachedInstance) {
        getDao().update(detachedInstance);
    }

    public Page<T> findByPage(Integer pageNo, Integer pageSize) {
        return getDao().findByPage(pageNo, pageSize);
    }

    public void executeHQL(String hql) {
        this.executeHQL(hql, null);
    }

    public void executeHQL(String hql, Object[] params) {
        getDao().executeHQL(hql, params);
    }

}
